﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Info;
using System.Windows.Navigation;
using Microsoft.Phone.Net.NetworkInformation;
using System.Xml.Linq;
using System.Collections.ObjectModel;
using System.Windows.Controls.Primitives;

namespace Nok_Air.BookFlight
{
    public partial class BookFlightPage : PhoneApplicationPage
    {
        private Popup popup;
        private string roundtripFlag = "0"; //default is Oneway=> 0==oneway,1==roundTrip
        public BookFlightPage()
        {
            popup = new Popup();
            popup.Child = new ProgressBarUserControl();
            InitializeComponent();
            Nok_Air.App.ViewModel.ScheduleItems = new System.Collections.ObjectModel.ObservableCollection<NokObjects.ScheduleItemViewModel>();
            DataContext = App.ViewModel;

            //add number for seat
            this.adultSeat.ItemsSource = new List<string>() { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" };
            this.childSeat.ItemsSource = new List<string>() { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" };

            //load airport list at startup
            popup.IsOpen = true;
            Nok_Air.WebService.NokAirServicesHelper nokSHelp = new Nok_Air.WebService.NokAirServicesHelper();
            nokSHelp.GetAvailableAirportCompleted += nokSHelp_RequestCompleted;
            nokSHelp.GetAvailableAirports();
        }

        void nokSHelp_RequestCompleted(object sender, WebService.RequestCompletedEventArgs ev)
        {
            if (ev.exception != null)
            {
                Dispatcher.BeginInvoke(() =>
                {
                    MessageBox.Show("Connection Error");
                    NavigationService.GoBack();
                    NavigationService.Navigated += (ss, ee) =>
                    {
                        popup.IsOpen = false;
                    };
                });
                return;
            }
            Dispatcher.BeginInvoke(() =>
            {
                popup.IsOpen = false;
            });
        }
        //when src airport selection changed(include startup) 
        private void airportScr2_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            popup.IsOpen = true;
            Nok_Air.WebService.NokAirServicesHelper nokSHelp = new Nok_Air.WebService.NokAirServicesHelper();
            nokSHelp.GetDestinationsCompleted += nokSHelp_RequestCompleted;
            nokSHelp.GetDestinations(Nok_Air.App.ViewModel.AvailableAirportItemsSrc[(sender as ListPicker).SelectedIndex].AirportCode);
        }

        private void dateFlightDepart_ValueChanged(object sender, DateTimeValueChangedEventArgs e)
        {
            dateFlightReturn.Value = (sender as DatePicker).Value;
        }

        private void oneway_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            roundtripFlag = "0";
            tbRetun.Visibility = System.Windows.Visibility.Collapsed;
            dateFlightReturn.Visibility = System.Windows.Visibility.Collapsed;
        }

        private void roundtrip_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            roundtripFlag = "1";
            tbRetun.Visibility = System.Windows.Visibility.Visible;
            dateFlightReturn.Visibility = System.Windows.Visibility.Visible;
        }

        private void bntNextSummary_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (adultSeat.SelectedItem.ToString() == "0" && childSeat.SelectedItem.ToString() == "0")
            {
                return;
            }
            string numseat = (int.Parse(adultSeat.SelectedItem.ToString()) + int.Parse(childSeat.SelectedItem.ToString())).ToString();

            DateTime dtDepart = (DateTime)dateFlightDepart.Value.Value;
            DateTime dtReturn = (DateTime)dateFlightReturn.Value.Value;

            string origin = Nok_Air.App.ViewModel.AvailableAirportItemsSrc[airportScr2.SelectedIndex].AirportCode;
            string destination = Nok_Air.App.ViewModel.AvailableAirportItemsDest[airportDest2.SelectedIndex].AirportCode;

            popup.IsOpen = true;
            Nok_Air.WebService.NokAirServicesHelper nokSHelp = new Nok_Air.WebService.NokAirServicesHelper();
            nokSHelp.GetAvailabilityCompleted += nokSHelp_RequestCompleted;
            nokSHelp.GetAvailabilityCompleted += (s, ev) =>
            {
                if (ev.exception == null)
                {
                    int count = App.ViewModel.AvailabilityItems.Sum(item => { 
                        return item.FlightSegmentItem.Count; });
                    if (count == 0)
                    {
                        Dispatcher.BeginInvoke(() => {
                            MessageBox.Show(App.ViewModel.NoFlights);
                            NavigationService.GoBack();
                        });
                    }
                }
            };
            nokSHelp.GetAvailability(origin, destination, dtDepart, dtReturn, roundtripFlag, numseat);
            NavigationService.Navigate(new Uri("/BookFlight/2SelectFlights.xaml?PassengerInfo=ADULT=" + adultSeat.SelectedItem.ToString() + ",CHD=" + childSeat.SelectedItem.ToString() + "&roundtripFlag=" + roundtripFlag, UriKind.Relative));
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            if (NavigationService.CanGoBack && App.ViewModel.IsFinishTransaction)
            {
                NavigationService.GoBack();
            }
        }

        private void airportDest2_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }
    }
}